package com.twu.biblioteca;

import java.util.Iterator;
import java.util.TreeMap;

public class App implements Iterable<Integer> {

    public static final String SELECT_VALID_OPTION = "Select Valid Option!!\n";
    private static final TreeMap<Integer, MenuOption> mainMenu = new TreeMap<Integer, MenuOption>();
    private static BookShelf bookShelf;
    private MovieShelf movieShelf;

    public App(BookShelf bookShelf, MovieShelf movieShelf) {
        this.bookShelf = bookShelf;
        this.movieShelf = movieShelf;
        populateMainMenu();
    }

    private void populateMainMenu() {
        mainMenu.put(1, new ViewBookList("View Book List",bookShelf));
        mainMenu.put(2, new ViewMovies("View Movies", movieShelf));
        mainMenu.put(3, new MembershipDetails("View Membership Details",null));
        mainMenu.put(4, new LoginUser("Login User"));
    }

    public static void createUserSession(User user){
        mainMenu.put(3, new MembershipDetails("View Membership Details",user));
        mainMenu.put(5, new ReserveBook("Reserve Book For Collection", bookShelf));
    }


    public String getMainMenuResponse(int input) {
        if(hasMenuOption(input)){
            MenuOption menuOption = mainMenu.get(input);
            return menuOption.getResponse();
        }else{
            return SELECT_VALID_OPTION;
        }
    }

    public boolean hasMenuOption(int input) {
        return mainMenu.get(input) != null ? true : false;
    }

    public String getWelcomeMessage() {
        return  "Welcome to Biblioteca\n" +
                "______________________________________\n";
    }

    public String getMainMenuList() {
        String menuList="";
        for (Integer option : mainMenu.keySet()) {
            menuList+=option.toString()+"-"+mainMenu.get(option).getName()+"\n";
        }
        return mainMenuHeaderMessage()+menuList;
    }

    private String mainMenuHeaderMessage(){
           return "Please Select Options Below\n";
    }

    @Override
    public Iterator<Integer> iterator() {
        return mainMenu.keySet().iterator();
    }
    private MenuOption getOption(int option){
        return mainMenu.get(option);
    }

    public boolean hasSubMenuFor(int option) {
        if(getOption(option)!=null) return getOption(option).hasSubMenu();
        return false;
    }

    public String getSubMenuResponse(int option, int option2) {
        MenuOption menuOption = getOption(option);
        if(menuOption!=null) return menuOption.getSubMenuResponse(option2);
        return "";
    }



}
